package com.supsi.segreteria.client;

import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * User: piccioni & bertoldi
 * Date: 20.04.12
 * Time: 16:35
 *
 * Description: Main thread that initiate Gui and Connection engine
 * Gui use a configurator instance to set client parameters, with a
 * property files saved in the same folder as the executable.
 *
 * Communication is a singleton class and implements an observer
 * design pattern to communicate with the Gui in asyncronous mode
 */
public class SegreteriaClient implements IClientObserver {
    private String state = "";

    public static void main(String[] args) {
        SegreteriaClient segreteriaClient  = new SegreteriaClient();
    }

    public SegreteriaClient() {

        //load config (if existing )
        Configurator configurator = Configurator.getInstance();

        //instantiate network objects
        ClientCommunicationManager clientCommunicationManager = ClientCommunicationManager.getInstance();
        Thread t = new Thread(ClientCommunicationManager.getInstance());
        t.start();
        //instantiate net thread for communication
        clientCommunicationManager.addObserver(this);

        //show the GUI
        GUIView guiView = new GUIView();
        JFrame chatFrame = new JFrame("Segreteria");
        chatFrame.setContentPane(guiView.getContentPanel());
        chatFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        chatFrame.pack();
        chatFrame.setVisible(true);
    }

    /**
     * update observer class implemented just to check functionality of observer
     * outputs state on the console (System.out.println)
     *
     * @param o contain ad object getState() that returns a string with the last state of the server communication
     *
     */
    @Override
    public void update(IClientSubject o) {
        state = o.getState();
        System.out.println("Update received, state changed to : " + state);
    }
}
